function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
var Request = new Object();
Request = GetRequest();


var url = document.location.toString();
var arrUrl = url.split("//");
var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(0,start);


var $phoneX = $('#phoneX');
var $phoneCode = $('#phoneCode');
var $phoneTimer = $('#phoneTimer');
var $payBtn = $('#payBtn');


$(function(){
    countTime.start();
})


$phoneCode.on('input propertychange',function(){
    var codeLen = $(this).val().length;
    console.log(codeLen);
    codeLen>0?$phoneX.show():$phoneX.hide();
});

$phoneX.on('click',function(){
    $phoneCode.val('');
});

$phoneTimer.on('click',function(){
    console.log('$phoneTimer');
    var isReady = countTime.getStatus();
    if(!isReady)return false;
    countTime.start();
});

$payBtn.on('click',function(){

});


// $phoneTimer
var countTime = (function(){
    var times = 60;
    var timer = '';
    var isReady = false;
    return {
        getStatus:function(){
            return isReady;
        },
        restart:function(){
            times = 60;
            isReady = true;
            countTime.stop();
            countTxt = '重新获取';
            $phoneTimer.text(countTxt).addClass('on');
        },
        start :function(){
            $phoneTimer.text('('+times +') 重新获取').removeClass('on');
            timer = setInterval(function(){
                var countTxt = '';
                times--;
                if(times>0){
                    isReady = false;
                    countTxt = '('+times+') 重新获取';
                    $phoneTimer.text(countTxt).removeClass('on');
                }else{
                    countTime.restart();
                }
            },1000);
        },
        stop : function(){
            clearInterval(timer);
        },
    }
})();



//返回上一页
function returnBack(){
    //调用原生的返回上一页方法
    if(checkOS()=="Android")
    {
        maojia.mjGoBack();
    } else{
    setupWebViewJavascriptBridge(function(bridge) {
        bridge.callHandler('mjGoBack', function(response) {
        })
    })
    }

}


//判断是android还是IOS系统
function checkOS() {
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
        return "IOS";
    } else if (/android/.test(ua)) {
        return "Android";
    }
}
//o家APP初始ios 注册
function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
//ios调用
setupWebViewJavascriptBridge(function(bridge) {
    bridge.registerHandler('testJSFunction', function(data, responseCallback) {
        responseCallback('js执行过了');
    })
})


function pageAlert(txt,title){
    txt = txt ||'';
    title = title ||'提示';
    var $body =$('body');
    var $alert = $body.find('#alertPanel');
    if(!$alert.length){
        var html =  '<div class="page-alert-wrapper" id="alertPanel">'+
            '<span class="page-jumbotron" id="alertJumbotron"></span>'+
            '<div class="page-alert">'+
            '<header class="page-title" id="alertTitile">提示</header>'+
            '<div class="page-content" id="alertContent">'+
            '</div>'+
            '<footer class="page-footer"  id="alertBtn">确认</footer>'+
            '</div></div>';
        $body.append(html);
        $alert = $body.find('#alertPanel');
    }

    $('#alertTitile').text(title);   //设置标题
    $('#alertContent').text(txt);    //设置中间内容
    $('#alertPanel').show();    //弹框显示
    $('#alertPanel').off().on('click','#alertBtn,#alertJumbotron',function(){
        $('#alertPanel').hide();
    })
}

/***************************确认注册重发短信**********************************/
function Resend(){
    var cupGuid = $("#cupGuid").val();
    $.ajax({
        url:'/payment/ResendPaySMS',
        type:'POST', //GET
        async:true,    //或false,是否异步
        contentType: 'application/json;charset=UTF-8',
        data:JSON.stringify({
            guid:cupGuid
        }),
        dataType:'text',
        success:function(data){

        },
        error:function(xhr,textStatus,errorThrown){
            //alert("签约失败");
            pageAlert("短信获取失败");
        }
    })
}
/***************************确认绑定**********************************/

function confirmPay(){
    //cupguid
    var cupGuid = $("#cupGuid").val();
    //短信验证码
    var phoneCode = $("#phoneCode").val();
    $.ajax({
        url:'/payment/confirmPay',
        type:'POST', //GET
        async:true,    //或false,是否异步
        contentType: 'application/json;charset=UTF-8',
        data:JSON.stringify({
            guid:cupGuid,
            smscode:phoneCode
        }),
        dataType:'json',
        success:function(data){
            console.log(data)
            console.log(data.Guid)
            var dataguid = data.Guid;
            var errorCode = data.errorCode;
             if('3045' == errorCode){
                 pageAlert("卡上的余额不足");

             }else if ('2000' == errorCode ||'2008' == errorCode){
                 var paymentTex = data.payguid;
                 window.open(arrUrl[0] + "//" + relUrl + "/payment/payWait?paymentId"+paymentTex,"_self");
             }else if ('4444' == errorCode){
                 window.open(arrUrl[0] + "//" + relUrl + "/payment/payError","_self");
             } else if ('3057' == errorCode){
                 pageAlert("验证码有误，请重新获取");
            }else if ('3058' == errorCode){
                 pageAlert("验证码有误，请重新获取");
             }else if ('3059' == errorCode){
                pageAlert("验证码有误，请重新获取");
            }else  if ('0000' == errorCode){
                 if(checkOS()=="Android"){  //加入原生
                     maojia.paystate(true);
                 } else {
                     setupWebViewJavascriptBridge(function(bridge) {
                         bridge.callHandler('paystate',{result:true}, function(response) {
                         })
                     });
                 }
                 window.open(arrUrl[0] + "//" + relUrl + "/payment/paySuccess","_self");
             }else{
                 if(checkOS()=="Android"){  //加入原生
                     maojia.paystate(false);
                 } else {
                     setupWebViewJavascriptBridge(function(bridge) {
                         bridge.callHandler('paystate',{result:false}, function(response) {
                         })
                     });
                 }
                 window.open(arrUrl[0] + "//" + relUrl + "/payment/payError","_self");
             }
        },
        error:function(xhr,textStatus,errorThrown){

            if(checkOS()=="Android"){  //加入原生
                maojia.paystate(false);
            } else {
                setupWebViewJavascriptBridge(function(bridge) {
                    bridge.callHandler('paystate',{result:false}, function(response) {
                    })
                });
            }
            window.open(arrUrl[0] + "//" + relUrl + "/payment/payError","_self");
        }


    })
}